package com.kly.cashmall.utils.function;

import android.app.Activity;
import android.os.Handler;
import android.os.Looper;

import com.kly.cashmall.R;

import java.lang.ref.SoftReference;

/**
 * @author zxp
 * @since 27/10/21
 */
public class PressHandle {

    private SoftReference<Activity> activity;
    private boolean isPress = false;

    public PressHandle(Activity activity) {
        this.activity = new SoftReference(activity);
    }

    public boolean handlePress(int keyCode) {
        if (keyCode == 4 && this.activity.get() != null) {
            if (!this.isPress) {
                this.isPress = true;
                ToastUtil.showToast(((Activity)this.activity.get()).getResources().getString(R.string.exit_tip));
                (new Handler(Looper.getMainLooper())).postDelayed(new Runnable() {
                    public void run() {
                        PressHandle.this.isPress = false;
                    }
                }, 1500L);
                return true;
            } else {
                this.isPress = false;
                return false;
            }
        } else {
            return false;
        }
    }

}
